<?php
namespace app\mobile\controller;
use think\Controller;
use think\Session;
class User extends Controller
{
	public function index()
	{
		if (!session('user_id','','mobile')){
			$this->assign('iflogin','never');
		}else {
			$this->assign('iflogin','has');
		}
		return $this->fetch('index');
	}
	
	public function login_view()
	{
		return $this->fetch('login_view');
	}
	
	public function login()
	{
		$result = array('err' => '1','content' => '');
		$password = trim(input('post.password'));
		$password = md5($password);
		$username = trim(input('post.username'));
		$user_info = db('user')->field(['id','user_name','usertype'])->where(['user_name'=>$username,'password'=>$password])->find();
		if (empty($user_info)){
			$user_info = db('ouser')->where(['user_name'=>$username,'password'=>$password])->find();
		}
		if (empty($user_info)){
			$result['content'] = '请检查是否账号和密码是否正确！';
			return $result;
		}
		if ($user_info['usertype'] == 'ordinary'){
			$loginlog = model('Loginlog');
			$save_login_log = $loginlog->saveLoginLog($user_info);
		}
		session('user_id',$user_info['id'],'mobile');
		session('role',$user_info['usertype'],'mobile');
		session('user_warehouse',isset($user_info['warehouse_id']) ? $user_info['warehouse_id']:0,'mobile');
		$result['err'] = '0';
		$result['content'] = '登录成功！';
		return $result;	
	}
	
	public function logout()
	{
		$result = array('err' => '1','content' => '');
		Session::delete('user_id','mobile');
		Session::delete('role','mobile');
		Session::delete('user_warehouse','mobile');
		$result['err'] = '0';
		$result['content'] = '退出成功！';
		return $result;
		
	}
}